#ifndef mysql_conn_h__
#define mysql_conn_h__

#define _CRT_SECURE_NO_WARNINGS

#include <string>
#include "dbpool.h"

#ifdef WIN32
#include "winsock2.h"
#pragma comment(lib,"ws2_32")
#endif

#ifdef WIN32
#include "mysql.h"
#include "errmsg.h"
#else
#include <mysql.h>
#include <errmsg.h>
#endif

class mysql_conn
{
public:
	static bool connect(mysql_conn* conn, const std::string& ip, const std::string& user, const std::string& pwd, const std::string& db, const unsigned short port);
public:
	mysql_conn();
	~mysql_conn();

	bool connect();
	bool lockTable(const char* table, const char* priority);
	bool unlockTable();
	bool query(const char *sql);
	bool isResEmpty();
	bool connected();

	MYSQL_RES* genRes();
	MYSQL* get_mysql();
	MYSQL_RES* getRes() { return m_queryRes; }

public:
	void setConfig(const std::string& ip, const std::string& user, const std::string& pwd, const std::string& db, const int port);

private:
	MYSQL m_mysql;
	MYSQL_ROW m_row;
	MYSQL_RES *m_queryRes;
	MYSQL_FIELD *m_field;

	std::string _host;
	std::string _user;
	std::string _passwd;
	std::string _db;
	unsigned short _port;
};
#endif // mysql_conn_h__